/*
 * bencoderTest.c
 *
 *  Created on: 05/07/2009
 *      Author: andre
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "aatracker.h"
#include "trackerlogger.h"
#include "bencoder.h"

extern int LOG_LEVEL;

int testBencodeAnnounceResponse(){

    int errorCounter =0;

    AnnounceResults ar;
    BencodedResponse br;

    ar.complete=5;
    ar.incomplete=10;
    ar.numberOfPeers=1;

    /*1o peer*/
    ar.peers[0]='a';
    ar.peers[1]='b';
    ar.peers[2]='c';
    ar.peers[3]='d';
    ar.peers[4]='e';
    ar.peers[5]='f';

    /*2o peer*/
    ar.peers[6]='1';
    ar.peers[7]='2';
    ar.peers[8]='3';
    ar.peers[9]='4';
    ar.peers[10]='5';
    ar.peers[11]='6';


    bencodeAnnounceResponse(&ar, &br);

    if (memcmp(br.str, "d8:intervali3600e8:completei5e10:incompletei10e5:peers6:abcdefee", 64) !=0){
        puts ("1: error testing bencodeAnnounceResponse: response generated is incorrect");
        errorCounter++;
    }

    ar.numberOfPeers=2;
    bencodeAnnounceResponse(&ar, &br);
    if (memcmp(br.str, "d8:intervali3600e8:completei5e10:incompletei10e5:peers12:abcdef123456ee", 71) !=0){
            puts ("2: error testing bencodeAnnounceResponse: response generated is incorrect");
            errorCounter++;
    }

    ar.numberOfPeers=0;
    bencodeAnnounceResponse(&ar, &br);
    if (memcmp(br.str, "d8:intervali3600e8:completei5e10:incompletei10e5:peers0:ee", 58) !=0){
            puts ("3: error testing bencodeAnnounceResponse: response generated is incorrect");
            errorCounter++;
    }
    ar.numberOfPeers=2;
    ar.peers[0]='\0';

    int result= bencodeAnnounceResponse(&ar, &br);
    if (result != PEER_LIST_IS_NULL){
        puts ("3: error testing bencodeAnnounceResponse: generating response for null peerlists");
        errorCounter++;
    }

    if (errorCounter ==0){
        puts("bencodeAnnounceResponse OK");
    }

    return OK;
}

int runBencoderTestSuite(){

    LOG_LEVEL = CRITICAL;

    puts("Iniciando Suite de Testes de BEncoder \n");
    testBencodeAnnounceResponse();
    puts("Suite de Testes de BEncoder finalizada\n");

    return OK;
}


